package com.ludashi.weather;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @author : xfhy
 * Create time : 2021/3/1 13:30
 * Description :
 */
public class BrokenLineView extends RecyclerView {

    private int maxValue;
    private int minValue;
    private List<Integer> data1 = new ArrayList<>();
    private List<Integer> data2 = new ArrayList<>();
    private List<Integer> dataAll = new ArrayList<>();
    private Adapter adapter;

    public BrokenLineView(Context context) {
        super(context);
        init();
    }


    private void init() {
        setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        adapter = new Adapter();
        setAdapter(adapter);
    }

    public void setData1(List<Integer> d) {
        if (data1 != null) {
            data1.clear();
            data1.addAll(d);

            dataAll.clear();
            dataAll.addAll(d);

            Collections.sort(dataAll);
            minValue = dataAll.get(0);
            maxValue = dataAll.get(dataAll.size() - 1);
            adapter.notifyDataSetChanged();
        }
    }

    public void setData2(List<Integer> d) {
        if (data2 != null) {
            data2.clear();
            data2.addAll(d);

            dataAll.addAll(d);
            Collections.sort(dataAll);

            minValue = dataAll.get(0);
            maxValue = dataAll.get(dataAll.size() - 1);
            adapter.notifyDataSetChanged();
        }
    }

    class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {


        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            View view = layoutInflater.inflate(R.layout.weather_item, parent, false);

            /*Item item = new Item(getContext());
            item.setMinValue(minValue);
            item.setMaxValue(maxValue);
            LayoutParams layoutParams = new LayoutParams(200, ViewGroup.LayoutParams.MATCH_PARENT);//这个数字表示每一个item的宽度
            item.setLayoutParams(layoutParams);*/

            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            if (position == 0) {
                holder.item1.setDrawLeftLine(false);
                holder.item2.setDrawLeftLine(false);
            } else {
                holder.item1.setDrawLeftLine(true);
                holder.item1.setlastValue((data1.get(position - 1)));
                holder.item2.setDrawLeftLine(true);
                holder.item2.setlastValue((data2.get(position - 1)));
            }
            holder.item1.setCurrentValue((data1.get(position)));
            holder.item2.setCurrentValue((data2.get(position)));


            if (position == data1.size() - 1) {
                holder.item1.setDrawRightLine(false);
            } else {
                holder.item1.setDrawRightLine(true);
                holder.item1.setNextValue((data1.get(position + 1)));
            }

            if (position == data2.size() - 1) {
                holder.item2.setDrawRightLine(false);
            } else {
                holder.item2.setDrawRightLine(true);
                holder.item2.setNextValue((data2.get(position + 1)));
            }

        }

        @Override
        public int getItemCount() {
            return data1.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            Item item1;
            Item item2;

            public ViewHolder(View itemView) {
                super(itemView);
                item1 = itemView.findViewById(R.id.item1);
                item2 = itemView.findViewById(R.id.item2);
                item1.setMinValue(minValue);
                item1.setMaxValue(maxValue);
                item1.setPaintColor(0xffFA1416);
                item2.setMinValue(minValue);
                item2.setMaxValue(maxValue);
                item2.setPaintColor(0xff1673FF);
            }
        }
    }


}
